﻿using AutoMapper;
using JTTD.DataBase;
using JTTD.Domain.Dtos;
using JTTD.Domain.Dtos.CompanyBill.Request;
using JTTD.Domain.Dtos.CompanyBill.Response;
using JTTD.Domain.IService;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JTTD.Common.Helper;
using JTTD.DataBase.Model.CompanyBill;

namespace JTTD.Domain.Service
{
    /// <summary>
    /// 店家账单服务
    /// </summary>
    public class CompanyBillService : ICompanyBillService
    {
        private readonly JTTDContext _context;
        private readonly IMapper _mapper;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="mapper"></param>
        public CompanyBillService(JTTDContext context, IMapper mapper)
        {
            _context = context;
            _mapper = mapper;
        }

        /// <summary>
        /// 获取指定店铺没有支付的账单
        /// </summary>
        /// <param name="shopId"></param>
        /// <returns></returns>
        public async Task<ApiResult<List<CompanyBillResponseDto>>> GetNoPayBillList(int shopId)
        {
            var list = await _context.CompanyBillModels.Where(x => x.ShopId == shopId&&!x.IsPay&&!x.IsDelete).OrderByDescending(item=>item.PayDate).ToListAsync();
            return new ApiResult<List<CompanyBillResponseDto>>()
            {
                IsSuccess = true,
                Data = _mapper.Map<List<CompanyBillResponseDto>>(list)??new List<CompanyBillResponseDto> ()
            };
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="shopId"></param>
        /// <param name="search"></param>
        /// <returns></returns>
        public async Task<ApiResult<PageResult<CompanyBillResponseDto>>> GetPageList(int shopId,SearchCompanyBillDto search)
        {
            try
            {
                var query = _context.CompanyBillModels.Where(item=>item.ShopId==shopId&&!item.IsDelete);
                int count = await query.CountAsync();
                if (count > 0)
                {
                    var list = await query.OrderByDescending(item=>item.PayDate).Skip((search.PageIndex - 1) * search.PageSize).Take(search.PageSize).ToListAsync();
                    return new ApiResult<PageResult<CompanyBillResponseDto>>()
                    {
                        IsSuccess = true,
                        Data = new PageResult<CompanyBillResponseDto>
                        {
                            Data = _mapper.Map<List<CompanyBillResponseDto>>(list),
                            PageIndex = search.PageIndex,
                            PageSize = search.PageSize,
                            TotalNum = count,
                            TotalPageNum = (count % search.PageSize) == 0 ? count / search.PageSize : count / search.PageSize + 1
                        }
                    };
                }
                else
                {
                    return new ApiResult<PageResult<CompanyBillResponseDto>>()
                    {
                        Data = new PageResult<CompanyBillResponseDto>()
                        {
                            PageIndex = search.PageIndex,
                            PageSize = search.PageSize,
                            Data = new List<CompanyBillResponseDto>()
                        },
                        IsSuccess = true,
                        Message = "查询成功，暂无数据"
                    };
                }
            }
            catch (Exception ex)
            {
                return new ApiResult<PageResult<CompanyBillResponseDto>>()
                {
                    IsSuccess = false,
                    Message = "查询失败，" + ex.Message
                };
            }
        }

        /// <summary>
        /// 设置店家账单
        /// </summary>
        /// <param name="shopId"></param>
        /// <param name="dtos"></param>
        /// <returns></returns>
        public async Task<ApiResult<bool>> SetCompanyBill(int shopId,List<SetCompanyBillRequestDto> dtos)
        {
            try
            {
              
                var models = _mapper.Map<List<CompanyBillModel>>(dtos);
                foreach(var item in models)
                {
                    item.ShopId = shopId;
                }
                _context.CompanyBillModels.AddRange(models.ToArray());
                await _context.SaveChangesAsync();
                return new ApiResult<bool> { IsSuccess = true, Message = "操作成功" };
            }
            catch(Exception ex)
            {
                return new ApiResult<bool> { IsSuccess=false,Message="操作失败，"+ex.Message};
            }
          
        }

        /// <summary>
        /// 作废账单
        /// </summary>
        /// <param name="billId"></param>
        /// <returns></returns>
        public async Task<ApiResult<bool>> DeleteCompanyBill(int billId)
        {
            try
            {
                var bill = await _context.CompanyBillModels.FindAsync(billId);
                if (bill == null)
                {
                    return new ApiResult<bool> { IsSuccess = false, Message = "没有找到指定条件的账单" };
                }
                bill.IsDelete = true;
                _context.CompanyBillModels.Update(bill);
                await _context.SaveChangesAsync();
                return new ApiResult<bool> { IsSuccess = true, Message = "操作成功" };
            }
            catch(Exception ex)
            {
                return new ApiResult<bool> { IsSuccess=false,Message="操作失败，"+ex.Message};
            }
           
        }

        /// <summary>
        /// 确认缴费
        /// </summary>
        /// <param name="billId"></param>
        /// <returns></returns>
        public async Task<ApiResult<bool>> ConfirmBill(int billId)
        {
            try
            {
                var bill = await _context.CompanyBillModels.FindAsync(billId);
                if (bill == null)
                {
                    return new ApiResult<bool> { IsSuccess = false, Message = "没有找到指定条件的账单" };
                }
                bill.IsPay = true;
                _context.CompanyBillModels.Update(bill);
                await _context.SaveChangesAsync();
                return new ApiResult<bool> { IsSuccess = true, Message = "操作成功" };
            }
            catch (Exception ex)
            {
                return new ApiResult<bool> { IsSuccess = false, Message = "操作失败，" + ex.Message };
            }
        }


    }
}
